package com.zjgsu.coursecalendar.service.impl;

import com.zjgsu.coursecalendar.model.User;
import com.zjgsu.coursecalendar.repository.UserRepository;
import com.zjgsu.coursecalendar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
/*    @Override
    public User getUserByuserid(int userid) {
        return null;
    }*/

    @Override
    public Integer login(User user) {
        User users =  userRepository.findByUserAccountAndAndUserPassword(user.getUserAccount(),user.getUserPassword());
        if (users==null){
            return 0;
        }else {
            return users.getRoleId();
        }

    }

    @Override
    public Integer getUserId(User user) {
        User user1 = userRepository.findByUserAccountAndAndUserPassword(user.getUserAccount(),user.getUserPassword());
        if (user1==null){
            return 0;
        }else {
            return user1.getUserId();
        }
    }

    @Override
    public void add(User user) {
        userRepository.save(user);
    }

    @Override
    public void delete(Integer id) {
        if (userRepository.findOne(id)!=null){
            userRepository.delete(id);
        }
    }

    @Override
    public void updata(User user, Integer id) {
        if (userRepository.findOne(id)!=null){
            userRepository.save(user);
        }
    }

    @Override
    public User find(Integer userid) {
        return userRepository.findOne(userid);
    }
}
